@page "/Crd"
@using BlazorApp.Pages.Common
@using BlazorApp.Pages.Common.Html.HTable
@using BlazorApp.Pages.Common.Metadata
@using k8s.Models
@using Mapster
@inherits BlazorApp.Pages.Common.TableBase<V1CustomResourceDefinition>
@if (TableData.PagedItems != null)
{
    <HTable Class="wideTable">
        <HTr>
            <HTd >
                <SearchToolBar Title=@L["CustomResourceDefinition"] Count="@TableData.Total"
                               OnItemDeletedCallback="@(async () => await InvokeAsync(StateHasChanged))"
                               OnSelectedItemCloseClicked="@TableData.RemoveSelection"
                               OnRemoveAllClicked="@TableData.RemoveAllSelection" TableData="@TableData"
                               OnSearch="OnSearchHandler" TItem="V1CustomResourceDefinition"
                               OnNsSelected="OnNsSelectedHandler"
                               HideNsSelector="true">
                </SearchToolBar>
            </HTd>
        </HTr>
        <HTr>
            <HTd >
                <Table TItem="V1CustomResourceDefinition"
                       DataSource="@TableData.PagedItems"
                       Size=@TableSize.Small Bordered="true"
                       Total="TableData.Total"
                       @bind-PageIndex="TableData.PageIndex"
                       @bind-PageSize="TableData.PageSize"
                       @bind-SelectedRows="TableData.SelectedRows"
                       Loading="TableData.Loading"
                       OnChange="OnPageChangeHandler">
                    <Selection Key="@(context.Name())"/>
                    <AntDesign.Column Field="context.Name()" Title=@L["Name"]>
                        <Button OnClick="@(() => OnItemNameClick(context))" Type="@ButtonType.Link">@context.Name()</Button>
                    </AntDesign.Column>
                    <AntDesign.Column Title=@L["Group"] TData="string" Width="180px">
                        @($"{context.Spec.Group}")
                    </AntDesign.Column>
                    <AntDesign.Column Title=@L["GroupVersions"] TData="string" Width="100px">
                        @if (context.Spec.Versions is { Count: > 0 })
                        {
                            foreach (var v in context.Spec.Versions)
                            {
                                <ColorfulTag>@v.Name</ColorfulTag>
                            }
                        }
                    </AntDesign.Column>
                    <AntDesign.Column Title=@L["Kind"] TData="string" Width="100px">
                        @if (context.Spec.Names?.Kind?.Length > 0)
                        {
                            <ColorfulTag> @context.Spec.Names.Kind</ColorfulTag>
                        }
                    </AntDesign.Column>
                    <AntDesign.Column Title=@L["Plural"] TData="string" Width="100px">
                        @if (context.Spec.Names?.Plural?.Length > 0)
                        {
                            <ColorfulTag> @context.Spec.Names.Plural</ColorfulTag>
                        }
                    </AntDesign.Column>
                    <AntDesign.Column Title=@L["Scope"] TData="string" Width="120px">
                        @($"{context.Spec.Scope}")
                    </AntDesign.Column>
                    <AntDesign.Column Title=@L["Count"] TData="string" Width="100px">

                        @if (CrCount.Keys.Contains(context.Name()))
                        {
                            @CrCount[context.Name()]
                        }
                    </AntDesign.Column>
                    <AntDesign.Column Title=@L["Condition"] TData="string" Width="100px">
                        <ConditionsTagView Conditions="@(context.Status.Conditions.Adapt<IList<V1Condition>>())"></ConditionsTagView>
                    </AntDesign.Column>
                    <AntDesign.Column Title=@L["Age"] TData="string" Width="100px">
                        <AgeView Age="@context.CreationTimestamp()"></AgeView>
                    </AntDesign.Column>
                    <ActionColumn Title=@L["Action"] Width="100px">
                        <CrdAction Item="@context"></CrdAction>
                    </ActionColumn>
                </Table>

            </HTd>
        </HTr>
    </HTable>

    <ResourceWatcher OnResourceChanged="OnResourceChanged" T="V1CustomResourceDefinition"></ResourceWatcher>
}
